<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="75px">
    <el-row>
      <el-form-item label="名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号/规格" prop="proModel">
        <el-input
          v-model="queryParams.proModel"
          placeholder="请输入型号/规格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性" prop="proProperty">
        <el-select v-model="queryParams.proProperty" placeholder="请选择" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in propertys"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="审核状态" prop="proFlag">
      <el-select v-model="queryParams.proFlag" placeholder="请选择" clearable size="small" @keyup.enter.native="handleQuery">
        <el-option
          v-for="item in auditStatus"
          :key="item.dictValue"
          :label="item.dictLabel"
          :value="item.dictValue">
        </el-option>
      </el-select>
      </el-form-item>
          <el-form-item  prop="proIsyin">
            <el-checkbox v-model="queryParams.proIsyin" true-label="1" false-label="0">含银</el-checkbox>
          </el-form-item>
        <el-form-item  prop="proIsjhx">
          <el-checkbox v-model="queryParams.proIsjhx" true-label="1" false-label="0">以旧换新</el-checkbox>
        </el-form-item>
          <el-form-item  prop="proIscbj">
            <el-checkbox v-model="queryParams.proIscbj" true-label="1" false-label="0">车间常备件</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>

      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sparepart:products:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sparepart:products:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sparepart:products:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productsList" @selection-change="handleSelectionChange" height="700">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="proName" />
      <el-table-column label="规格/型号 " align="center" prop="proModel" />
      <el-table-column label="警界上限" align="center" prop="proUpper" />
      <el-table-column label="警界下线" align="center" prop="proLower" />
      <el-table-column label="单位" align="center" prop="proUnitid" >
          <template slot-scope="scope">
            <span :key="item.dictValue" v-for="item in beiUnit" >
              <span v-if="scope.row.proUnitid===item.dictValue">{{item.dictLabel}}</span>
            </span>
          </template>
      </el-table-column>
      <el-table-column label="分类 " align="center" prop="beiTypes.typeName" >
      </el-table-column>
      <el-table-column label="备注说明" align="center" prop="proRemark" />
      <el-table-column label="维护人" align="center" prop="sysUser.nickName" />
      <el-table-column label="维护时间" align="center" prop="proTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.proTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="proFlag" >
        <template slot-scope="scope">
          <div :class="scope.row.proFlag===0 ?'statusStyle':'statusStyle1'"></div>
        </template>
      </el-table-column>
      <el-table-column label="确认人 " align="center" prop="checkUser.nickName">
        <template slot-scope="scope">
          <span v-show="scope.row.proFlag!==0">{{scope.row.checkUser.nickName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="确认时间" align="center" prop="proChecktime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.proChecktime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="属性" align="center" prop="proProperty" >
        <template slot-scope="scope">
           <span :key="item.dictValue" v-for="item in propertys" >
              <span v-if="scope.row.proProperty===item.dictValue">{{item.dictLabel}}</span>
            </span>
        </template>
      </el-table-column>
      <el-table-column label="含银" align="center" prop="proIsyin" >
        <template slot-scope="scope">
          <span v-text="getReplace(scope.row.proIsyin)"></span>
        </template>
      </el-table-column>
      <el-table-column label="以旧换新" align="center" prop="proIsjhx" >
        <template slot-scope="scope">
          <span v-text="getReplace(scope.row.proIsjhx)"></span>
        </template>
      </el-table-column>
      <el-table-column label="车间常备件" align="center" prop="proIscbj" >
        <template slot-scope="scope">
          <span v-text="getReplace(scope.row.proIscbj)"></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,scope.row.proFlag!==1?updateValue:detailValue)"
            v-hasPermi="['sparepart:products:edit']"
          >{{scope.row.proFlag!==1?'修改':'详情'}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sparepart:products:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.proFlag===0"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row,'备品备件货品审核')"
            v-hasPermi="['sparepart:products:check']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改备品备件货品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="fromdis">
        <el-row>
           <el-col :span="12">
              <el-form-item label="名称" prop="proName">
                <el-input v-model="form.proName" placeholder="请输入名称" />
              </el-form-item>
           </el-col>
          <el-col :span="12">
              <el-form-item label="规格 " prop="proModel">
                <el-input v-model="form.proModel" placeholder="请输入规格、型号 " />
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
            <el-form-item label="警界上限" prop="proUpper">
              <el-input-number v-model="form.proUpper" controls-position="right" :min="0" />
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="警界下线" prop="proLower">
              <el-input-number v-model="form.proLower" controls-position="right" :min="0" />
            </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位" prop="proUnitid">
              <el-select v-model="form.proUnitid" placeholder="请选择">
                <el-option
                  v-for="dict in beiUnit"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别" prop="typeId">
              <treeselect v-model="form.typeId"
                          :options="TypeList"
                          :normalizer="normalizer"
                          placeholder="请选择分类"
                          :disabled="fromdis"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="属性" prop="proProperty">
              <el-select v-model="form.proProperty" placeholder="请选择">
                <el-option
                  v-for="dict in propertys"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue">
                </el-option>

              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item  prop="proIsyin">
              <el-checkbox v-model="form.proIsyin" :true-label="1" :false-label="0" >含银</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  prop="proIsjhx">
              <el-checkbox v-model="form.proIsjhx" :true-label="1" :false-label="0">以旧换新</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  prop="proIscbj">
              <el-checkbox v-model="form.proIscbj" :true-label="1" :false-label="0">车间常备件</el-checkbox>
            </el-form-item>
          </el-col>
          <el-form-item label="备注说明" prop="proRemark">
            <el-input type="textarea" v-model="form.proRemark" placeholder="请输入备注说明" />
          </el-form-item>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.proFlag===0">
        <el-button type="success" @click="checkProduct" v-hasPermi="['sparepart:products:check']">通 过</el-button>
        <el-button type="danger" @click="checkDelete(form.proId)"  v-hasPermi="['sparepart:products:remove']">删 除</el-button>
        <el-button type="primary" @click="submitForm" v-hasPermi="['sparepart:products:affirm']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {checkProducts, listProducts, getProducts, delProducts, addProducts, updateProducts, exportProducts } from "@/api/sparepart/products";
import {listTypes} from '@/api/sparepart/types'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Products",
  components: {Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //分类树结构
      TypeList:[],
      // 备品备件货品表格数据
      productsList: [],
      //查询字典属性
      propertys:[],
      //查询字典单位
      beiUnit:[],
      //查字典审核状态
      auditStatus:[],
      // 弹出层标题
      title: "",
      //修改
      updateValue:'修改备品备件货品',
      //详情
      detailValue:'备品备件货品详情',
      // 是否显示弹出层
      open: false,
      //from表单禁用
      fromdis:false,
      // 查询参数
      queryParams: {
        proName: null,
        proModel: null,
        proUpper: null,
        proLower: null,
        proUnitid: null,
        typeId: null,
        proRemark: null,
        userId: null,
        proTime: null,
        proIsdel: null,
        proFlag: null,
        proCheckid: null,
        proChecktime: null,
        proProperty: null,
        proIsyin: null,
        proIsjhx: null,
        proIscbj: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proProperty:[{ required: true, message: "属性不能为空", trigger: "blur" }],
        proName:[{required: true, message: "货品不能为空", trigger: "blur"}],
        typeId:[{required: true, message: "分类不能为空", trigger: "blur"}],
        proUnitid:[{required: true, message: "单位不能为空", trigger: "blur"}]
      }
    };
  },
  created() {
    this.getList();
    this.getTypeList();
    //属性
    this.getDicts("pro_property").then(response => {
      this.propertys = response.data;

    });
    //单位
    this.getDicts("bei_unit").then(response=> {
      this.beiUnit = response. data;
    });
    //审核状态
    this.getDicts("audit_status").then(response=> {
      this.auditStatus = response. data;
    });
  },
  methods: {
    /** 查询备品备件货品列表 */
    getList() {
      this.loading = true;
      listProducts(this.queryParams).then(response => {
        this.productsList = response.rows;
        this.loading = false;
      });
    },
    //获取分类树结构
    getTypeList(){
      listTypes().then(res=>{
        this.TypeList = this.handleTree(res.data, "typeId","typeParentid");
      })
    },
    /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.typeId,
        label: node.typeName,
        children: node.children
      };
    },
    //替换数值
    getReplace(val){
      return val===0?'否':'是'
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        proId: null,
        proName: null,
        proModel: null,
        proUpper: null,
        proLower: null,
        proUnitid: null,
        typeId: null,
        proRemark: null,
        userId: null,
        proTime: null,
        proIsdel: null,
        proFlag: null,
        proCheckid: null,
        proChecktime: null,
        proProperty: null,
        proIsyin: null,
        proIsjhx: null,
        proIscbj: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加备品备件货品";
    },
    /** 修改按钮操作 */
    handleUpdate(row,val) {
      this.reset();
      const proId = row.proId || this.ids
      getProducts(proId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = val
      });
      if (row.proFlag===1){
        this.fromdis = true
      }else {
        this.fromdis = false
      }

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.proId != null) {
            updateProducts(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProducts(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const proIds = row.proId || this.ids;
      this.$confirm('是否确认删除选择的备品备件货品?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProducts(proIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有备品备件货品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProducts(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },

    //审核操作
    checkProduct(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          checkProducts(this.form).then(response => {
            this.msgSuccess("审核成功");
            this.open = false;
            this.getList();
          })
        }
      })
    },
    /** 审核删除按钮操作 */
    checkDelete(proId) {
      this.$confirm('是否确认删除选择的备品备件货品?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delProducts(proId);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
        this.open = false
      })
    },

  }
};
</script>
<style scoped>
  .statusStyle{
    margin: auto;
    width: 10px;
    height: 10px;
    border-radius: 30px;
    background-color: gold;
  }
  .statusStyle1{
    margin: auto;
    width: 10px;
    height: 10px;
    border-radius: 30px;
    background-color: green;
  }
</style>
